<?php
App::import('Controller', 'SagePay.Payment');
App::import('Config', 'SagePay.sage_pay_settings');
class OrdersController extends StoreAppController {
    
	var $name = 'StoreOrders';
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'view' => array('admin','manag')
	);
	var $Payment;
	var $SagePaySettings;
	
	public function beforeFilter(){
		parent::beforeFilter();
		
		//Instantiation
    	$this->Payment =& new PaymentController();
    	//Load model, components...
    	$this->Payment->constructClasses();
    	$this->Payment->SagePaySettings =& new SagePaySettings();
    
//		$this->layout = 'cms';
	}
	
	function index() {
		$this->StoreOrder->recursive = 0;
		$this->set('storeOrders', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid store order', true));
			$this->redirect(array('action' => 'index'));
		}

		$this->StoreOrder->recursive = 2;
		
		$this->set('storeOrder', $this->StoreOrder->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->StoreOrder->create();
			if ($this->StoreOrder->save($this->data)) {
				$this->Session->setFlash(__d('store','The store order has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store order could not be saved. Please, try again.', true));
			}
		}
		$users = $this->StoreOrder->User->find('list');
		$this->set(compact('users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__d('store','Invalid store order', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->StoreOrder->save($this->data)) {
				$this->Session->setFlash(__d('store','The store order has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store order could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->StoreOrder->read(null, $id);
		}
		$users = $this->StoreOrder->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid id for store order', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->StoreOrder->delete($id)) {
			$this->Session->setFlash(__d('store','Store order deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__d('store','Store order was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function order_successful(){
		$strCrypt= $this->data["crypt"];

		
		// Now decode the Crypt field and extract the results
		$strDecoded = $this->Payment->decodeAndDecrypt($strCrypt);
		$values = $this->Payment->getToken($strDecoded);
		
		$this->log(print_r($values,true),'array_confirm');
	}
	
	function order_failed(){
		$strCrypt= $this->data["crypt"];
		
		// Now decode the Crypt field and extract the results
		$strDecoded = $this->Payment->decodeAndDecrypt($strCrypt);
		$values = $this->Payment->getToken($strDecoded);
		$strStatus=$values['Status'];
		// Determine the reason this transaction was unsuccessful
		if ($strStatus=="NOTAUTHED")
			$strReason="You payment was declined by the bank.  This could be due to insufficient funds, or incorrect card details.";
		else if ($strStatus=="ABORT")
			$strReason="You chose to Cancel your order on the payment pages.  If you wish to change your order and resubmit it you can do so here. If you have questions or concerns about ordering online, please contact us at [your number].";
		else if ($strStatus=="REJECTED") 
			$strReason="Your order did not meet our minimum fraud screening requirements. If you have questions about our fraud screening rules, or wish to contact us to discuss this, please call [your number].";
		else if ($strStatus=="INVALID" or $strStatus=="MALFORMED")
			$strReason="We could not process your order because we have been unable to register your transaction with our Payment Gateway. You can place the order over the telephone instead by calling [your number].";
		else if ($strStatus=="ERROR")
			$strReason="We could not process your order because our Payment Gateway service was experiencing difficulties. You can place the order over the telephone instead by calling [your number].";
		else
			$strReason="The transaction process failed. Please contact us with the date and time of your order and we will investigate.";
		
		$this->log(print_r($values,true),'array_failed');
	}
}
?>